package cn.chinaflame.pagodas.service;

import cn.chinaflame.pagodas.entity.PaBadLights;
import cn.chinaflame.pagodas.entity.PaEmailLog;
import cn.chinaflame.pagodas.enums.CodeEnum;
import cn.chinaflame.pagodas.repository.PaBadLightsRepository;
import cn.chinaflame.pagodas.repository.VwBlessDetailRepository;
import cn.chinaflame.pagodas.utils.ParamPreconditions;
import cn.chinaflame.pagodas.utils.stl.DateUtils;
import cn.chinaflame.pagodas.vo.LightVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

/**
 *坏灯记录Service
 * @author admin
 */
@Service
public class PaBadLightsService{
	
	 private static final Logger log = LoggerFactory.getLogger(PaAgentService.class);

	    @Autowired
	    private JdbcTemplate jdbcTemplate;
	    @Autowired
	    private PaBadLightsRepository paBadLightsRepository;
	    
	
	/**
	 * 通过日期查询
	 * @param date
	 * @return
	 */
    public List<PaBadLights> findByCreatetime(String date) {
    	log.info("findByCreatetime:date:{}",date);
    	ParamPreconditions.checkDateByFmt(date, DateUtils.DATE_DEFAULT_PATTERN);
    	String sql = "select date_format(createtime,'%Y-%m-%d %H:%i') createtime,lightno,message,result from pa_bad_lights where date_format(createtime,'%Y-%m-%d')= date_format(?,'%Y-%m-%d')";
    	List<PaBadLights> list = jdbcTemplate.query(sql, new Object[] {date}, (RowMapper<PaBadLights>) (rs, rowNum) -> {
    		PaBadLights result = new PaBadLights();
			result.setCreatetime(rs.getString("createtime"));
			result.setLightno(rs.getString("lightno"));
			result.setMessage(rs.getString("message"));
			result.setResult(rs.getInt("result"	));
			return result;
		});
    	log.info("list.size()={}",list.size());
		return list;
	}
    
    /**
     * 通过灯号查询
     * @param lightno
     * @return
     */
    public List<PaBadLights> findByLightno(String lightno) {
    	log.info("findByLightno:lightno:{}",lightno);
    	ParamPreconditions.checkNotEmpty(lightno, CodeEnum.FAIL.getCode(), "灯号不能为空！");
    	String sql = "select * from pa_bad_lights where lightno like ?";
    	List<PaBadLights> list = jdbcTemplate.query(sql, new Object[] {lightno}, (RowMapper<PaBadLights>) (rs, rowNum) -> {
    		PaBadLights result = new PaBadLights();
			result.setCreatetime(rs.getString("createtime"));
			result.setLightno(rs.getString("lightno"));
			result.setMessage(rs.getString("message"));
			result.setResult(rs.getInt("result"	));
			return result;
		});
    	log.info("list.size()={}",list.size());
		return list;
	}
    
    /**
     * 通过消息查询
     * @param msg
     * @return
     */
    public List<PaBadLights> findByMessage(String msg) {
    	log.info("findByMessage:msg:{}",msg);
    	ParamPreconditions.checkNotEmpty(msg, CodeEnum.FAIL.getCode(), "坏灯信息不能为空！");
    	String sql = "select * from pa_bad_lights where message = ?";
    	List<PaBadLights> list = jdbcTemplate.query(sql, new Object[] {msg}, (RowMapper<PaBadLights>) (rs, rowNum) -> {
    		PaBadLights result = new PaBadLights();
			result.setCreatetime(rs.getString("createtime"));
			result.setLightno(rs.getString("lightno"));
			result.setMessage(rs.getString("message"));
			result.setResult(rs.getInt("result"));
			return result;
		});
    	log.info("list.size()={}",list.size());
		return list;
	
	}
    /**
     * 
     * @param light
     * @return
     */
    public int addBadLight(LightVO light){
    	log.info("addBadLight:light:{}",light);
    	String sql = "insert into pa_bad_lights(lightno,createtime,message,result)values(?,now(),?,?) ";
    	int result = jdbcTemplate.update(sql,new Object[] {light.getLightno(),light.getMessage(),light.getResult()});
    	log.info("result:{}",result);
		return result;
	}
    
   /**
    * 批量插入接口
    * @param lights
    * @return
    */
    public int[] batchAddBadLight(List<LightVO> lights){
    	log.info("batchAddBadLight:lights。size():{}",lights.size());
    	String sql = "insert into pa_bad_lights(lightno,createtime,message,result)values(?,now(),?,?) ";
    	int[] result = null;
		 try {
				result = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
					@Override
					public int getBatchSize() {
						return lights.size();
					}
					@Override
					public void setValues(java.sql.PreparedStatement ps, int i) throws SQLException {
						ps.setString(1, lights.get(i).getLightno());
						ps.setString(2,lights.get(i).getMessage());
						ps.setInt(3, lights.get(i).getResult());
					}
				});
		 } catch (Exception e) {
				log.info(e.getMessage());
			}
		 log.info("result:{}",result);
			return result;
	}
}
